﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.ScrumTool.Service.Interface;
using UIT.ScrumTool.DTO;
using UIT.ScrumTool.Service.Model;

namespace UIT.ScrumTool.Service.Business
{
    public partial class ControlService : IDocumentService
    {
        // Get All Document In DB
        public List<DocumentDto> DocumentGetByAll()
        {
            List<DocumentDto> lsdocumentDto = new List<DocumentDto>();
            try
            {
                List<Document> ls = dataContext.sp_Document_GetByAll().ToList();
                foreach (var document in ls)
                {
                    lsdocumentDto.Add(DocumentConvertDocumentDto(document));
                }
                return lsdocumentDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        // Get Document with Project Id
        public List<DocumentDto> DocumentGetByProjectId(int projectId)
        {
            List<DocumentDto> lsdocumentDto = new List<DocumentDto>();
            try
            {
                List<Document> ls = dataContext.sp_Document_GetByProjectId(projectId).ToList();
                foreach (var document in ls)
                {
                    lsdocumentDto.Add(DocumentConvertDocumentDto(document));
                }
                return lsdocumentDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        // Get Document In DB with ID
        public DocumentDto DocumentGetById(int id)
        {
            try
            {
                return DocumentConvertDocumentDto(dataContext.sp_Document_GetById(id).Single());
            }
            catch (Exception)
            {
                return null;
            }
        }

        // Get List Project in DB with Condition (top, where, order)
        public List<DocumentDto> DocumentGetByTop(string top, string where, string order)
        {
            List<DocumentDto> lsdocumentDto = new List<DocumentDto>();
            try
            {
                string query = "Select * from [Document]";
                if (!top.Equals(""))
                    query = " Select top " + top + " * from [Document] ";
                if (!where.Equals(""))
                    query += " where " + where + " ";
                if (!order.Equals(""))
                    query += " order by " + order;

                IEnumerable<Document> results = dataContext.ExecuteQuery<Document>(@query);
                List<Document> ls = results.ToList();
                foreach (var document in ls)
                {
                    lsdocumentDto.Add(DocumentConvertDocumentDto(document));
                }
                return lsdocumentDto;
            }
            catch (Exception)
            {
                return null;
            }
        }

        // Insert Document to DB
        public bool DocumentInsert(DocumentDto document)
        {
            try
            {
                dataContext.sp_Document_Insert(document.ProjectId, document.FileContent, document.FileThumbNail, document.TaskId, document.Url, document.FileName);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Update Document to DB
        public bool DocumentUpdate(DocumentDto document)
        {
            try
            {
                dataContext.sp_Document_Update(document.Id, document.ProjectId, document.FileContent, document.FileThumbNail, document.TaskId, document.Url, document.FileName);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Delete Document in DB
        public bool DocumentDelete(int id)
        {
            try
            {
                dataContext.sp_Document_Delete(id);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Delete Document with Project Id
        public bool DocumentDeleteProjectId(int projectId)
        {
            try
            {
                dataContext.sp_Document_DeleteProjectId(projectId);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Convert Document to DocumentDto
        public static DocumentDto DocumentConvertDocumentDto(Document document)
        {
            DocumentDto documentDto = new DocumentDto();
            documentDto.Id = document.Id;
            documentDto.ProjectId = document.ProjectId;
            documentDto.FileContent = document.FileContent;
            documentDto.FileThumbNail = document.FileThumbNail;
            documentDto.TaskId = (int)document.TaskId;
            documentDto.Url = document.URL;
            documentDto.FileName = document.FileName;
            return documentDto;
        }
    }
}
